<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        skillRel = [[1, 0]]
        console.log(1)
        function dos(skillRel,k) {
            var skillTree = {}
            for (var i = 0; i < skillRel.length; i++) {
                if (skillTree[skillRel[i][0]] == undefined) {
                    skillTree[skillRel[i][0]] = []
                }
                skillTree[skillRel[i][0]].push(skillRel[i][1])
            }
            function resort(arr1,arr2){
                var arr3 = arr2.filter(function (v) {
                    return arr1.indexOf(v) !== -1 // 利用filter方法来遍历是否有相同的元素
                })
                var result = arr2.concat(arr3).filter(function (v) {
                    return arr2.indexOf(v) === -1 || arr3.indexOf(v) === -1
                })
                return result
            }
            console.log(skillTree) 
            var start=[]
            var answer=[]
            for(var i=0;i<k;i++){
                if(skillTree[i]==undefined){
                    start.push(i)
                    answer.push(i)
                }
            }
            while(start.length!=0){
                for(var item in skillTree){
                    skillTree[item] = resort(start,skillTree[item])
                    console.log(skillTree)
                }
                start=[]
                for(var item in skillTree){
                   if(skillTree[item].length==0)
                   {start.push(Number(item))
                    answer.push(Number(item))
                    delete skillTree[item]
                }
                   
                }
            }
            if(Object.keys(skillTree)==0){
                console.log("学完了")
                return answer
            }else{
                console.log("学个p")
                return []
            }
        }
        var de = dos(skillRel,1)
    // write code here
    </script>
</body>

</html>